package com.jonniespratley.generators.templates
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	 * Template Manager allows access to all templates
	 * located inside of the generators templates folder.
	 * 
	 *  
	 * @author jonniespratley
	 * 
	 */	
	public class TemplateManager extends EventDispatcher
	{
		public static const ALTER_TABLE_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Alter_Table.txt';
		public static const ALTER_PRI_KEY_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Alter_Table.txt';
		public static const ALTER_FOR_KEY_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Alter_Table.txt';

		public static const CREATE_INDEX_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Create_Index.txt';
		public static const CREATE_TABLE_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Create_Table.txt';
		public static const CREATE_VIEW_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Create_View.txt';
		public static const DELETE_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Delete.txt';

		public static const DROP_INDEX_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Drop_Index.txt';
		public static const DROP_TABLE_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Dop_Table.txt';
		public static const DROP_VIEW_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Drop_View.txt';

		public static const GRANT_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Grant.txt';
		public static const INSERT_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Insert.txt';
		public static const REVOKE_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Revoke.txt';
		public static const SELECT_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Select.txt';
		public static const UPDATE_STATEMENT:String = 'com/jonniespratley/generators/templates/sql/Update.txt';

		/**
		 * Private property for storing the template string. 
		 */		
		private var _template:String;
	
		/**
		 * 
		 */		
		public static const TEMPLATE_ARRAY:Array = [
				 { label: 'Alter Table', data: TemplateManager.ALTER_TABLE_STATEMENT } ,
				 { label: 'Add Primary Key', data: TemplateManager.ALTER_PRI_KEY_STATEMENT } ,
				 { label: 'Add Foreign Key', data: TemplateManager.ALTER_FOR_KEY_STATEMENT } ,
				 { label: 'Create Index', data: TemplateManager.CREATE_INDEX_STATEMENT } ,
				 { label: 'Create Table', data: TemplateManager.CREATE_TABLE_STATEMENT } ,
				 { label: 'Create View', data: TemplateManager.CREATE_VIEW_STATEMENT } ,
				 { label: 'Delete', data: TemplateManager.DELETE_STATEMENT } ,
				 { label: 'Drop Index', data: TemplateManager.DROP_INDEX_STATEMENT } ,
				 { label: 'Drop Table', data: TemplateManager.DROP_TABLE_STATEMENT } ,
				 { label: 'Drop View', data: TemplateManager.DROP_VIEW_STATEMENT } ,
				 { label: 'Grant', data: TemplateManager.GRANT_STATEMENT } ,
				 { label: 'Insert', data: TemplateManager.INSERT_STATEMENT } ,
				 { label: 'Revoke', data: TemplateManager.REVOKE_STATEMENT } ,
				 { label: 'Select', data: TemplateManager.SELECT_STATEMENT } ,
				 { label: 'Update', data: TemplateManager.UPDATE_STATEMENT }
		];

		/**
		 * 
		 * 
		 */		
		public function TemplateManager(){}

		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get template():String{
			return _template;
		}
		
		/**
		 * 
		 * @param value
		 * 
		 */		
		public function set template(value:String):void{
			_template = value;
		}
		
		/**
		 * 
		 * @param value
		 * 
		 */		
		public function setTemplate( value:String ):void
		{
			_template = value;
		}

		/**
		 * 
		 * @return 
		 * 
		 */		
		public function getTemplate():String
		{
			return _template;
		}
		
		/**
		 * 
		 * @param whatTemplate
		 * 
		 */		
		public function loadTemplate( whatTemplate:String ):void
		{
			var temp:String;

			var urlRequest:URLRequest = new URLRequest( whatTemplate );
			var loader:URLLoader = new URLLoader( urlRequest );
				loader.addEventListener( Event.COMPLETE, onTemplateResult );

				loader.load( urlRequest );

				trace( 'Loading template from local filesystem' );
		}
		
		/**
		 * 
		 * @param whatTemplate
		 * @return 
		 * 
		 */		
		static public function load( whatTemplate:String ):String
		{
			var temp:String;

			var urlRequest:URLRequest = new URLRequest( whatTemplate );
			var loader:URLLoader = new URLLoader( urlRequest );
			var out:String;
				loader.addEventListener( Event.COMPLETE, function complete( event:Event ):void{
					out = event.currentTarget.data as String;
				} );
				loader.load( urlRequest );
			return out;
		}

		/**
		 * 
		 * @param event
		 * 
		 */		
		private function onTemplateResult( event:Event ):void
		{
			var t:String = event.currentTarget.data as String;

			this.setTemplate( t );
			this.dispatchEvent( new TemplateEvent( TemplateEvent.TEMPLATE_LOAD_SUCCESS, t ) );

			trace( 'dispatchEvent Template Event' );
		}
		
		
	}
}